package com.dlc.felear.xiaoensale.activity;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.customView.LoadingDialog;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.entity.StepMotorInfo;
import com.dlc.felear.xiaoensale.utils.HexUtil;
import com.itdlc.android.library.base.BaseActivity;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 电机控制页面
 */
public class StepMotorDebugActivity extends BaseActivity {
    private static final String TAG = "StepMotorDebugActivity";
    private static final int WHAT_CANCEL_DIALOG=2321;
    @BindView(R.id.et_speed)
    EditText etSpeed;
    @BindView(R.id.et_plus)
    EditText etPlus;
    @BindView(R.id.et_count)
    EditText etAconut;
    @BindView(R.id.et_a1)
    EditText etA;
    private TopbarHelper topbarHelper;
    private LoadingDialog loadingDialog;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_step_m;
    }

    @Override
    protected void initialView() {
        EventBus.getDefault().register(this);
        loadingDialog=new LoadingDialog(this,"正在执行..");
        topbarHelper = new TopbarHelper(this);
        topbarHelper.setTitle("电机控制");
    }


    @OnClick({R.id.tv_set_speed, R.id.tv_set_plus, R.id.tv_set_a, R.id.tv_set_aconut, R.id.btn_stop,
            R.id.btn_zz,R.id.btn_fz,R.id.btn_q_speed,R.id.btn_q_plus,R.id.btn_q_aconut})
    public void onItemClick(View view) {
        switch (view.getId()){
            case R.id.tv_set_speed:
                sendCmd(new StepMotorInfo(0,getEtValue(etSpeed)));
                break;
            case R.id.tv_set_plus:
                sendCmd(new StepMotorInfo(1,getEtValue(etPlus)));
                break;
            case R.id.tv_set_a:
                sendCmd(new StepMotorInfo(2,getEtValue(etA)));
                break;
            case R.id.tv_set_aconut:
                sendCmd(new StepMotorInfo(3,getEtValue(etAconut)));
                break;
            case R.id.btn_stop:
                sendCmd(new StepMotorInfo(4,0));
                break;
            case R.id.btn_zz:
                sendCmd(new StepMotorInfo(5,0));
                break;
            case R.id.btn_fz:
                sendCmd(new StepMotorInfo(6,0));
                break;
            case R.id.btn_q_speed:
                sendCmd(new StepMotorInfo(7,0));
                break;
            case R.id.btn_q_plus:
                sendCmd(new StepMotorInfo(8,0));
                break;
            case R.id.btn_q_aconut:
                sendCmd(new StepMotorInfo(9,0));
                break;
        }

    }

    private void sendCmd(StepMotorInfo stepMotorInfo){
        loadingDialog.show(500);
        EventBus.getDefault().post(stepMotorInfo,Constant.STEP_MOTOR_CMD);
    }

    private int getEtValue(EditText et){
        String tempS=et.getText().toString().trim();
        if(!TextUtils.isEmpty(tempS)){
           return Integer.parseInt(tempS);
        }
        return 0;
    }

    @Subscriber(tag=Constant.STEP_MOTOR_RESULT)
    private void recieveData(byte[] data){
        Log.e(TAG,"接收的指令信息:"+ HexUtil.bytes2HexString(data));
        if(data[1]==(byte) 3){
            queryInfoData(data);
        }else if(data[1]==(byte) 6){
            resultInfoData(data);
        }

    }

    private void resultInfoData(byte[] data){
        switch (data[3]){
            case (byte) 0x05:
                int speed = (int) HexUtil.hex2decimal(HexUtil.encodeHexStr(Arrays.copyOfRange(data, 4, 6)));
                Log.e(TAG,"设置速度:"+speed);
                break;
            case (byte) 0x03:
                break;
        }
    }


    private void queryInfoData(byte[] data){
        switch (data[2]){
            case (byte) 0x02:
                int plus = (int) HexUtil.hex2decimal(HexUtil.encodeHexStr(Arrays.copyOfRange(data, 3, 5)));
                Log.e(TAG,"脉冲数:"+plus);
                setEdMsg(etAconut,plus);
                break;
            case (byte) 0x06:
                int speed = (int) HexUtil.hex2decimal(HexUtil.encodeHexStr(Arrays.copyOfRange(data, 3, 5)));
                int plus1 = (int) HexUtil.hex2decimal(HexUtil.encodeHexStr(Arrays.copyOfRange(data, 7, 9)));
                setEdMsg(etSpeed,speed);
                setEdMsg(etPlus,plus1);
                Log.e(TAG,"脉冲数:"+plus1+" speed="+speed);
                break;
        }
    }

    private void setEdMsg(EditText et,int value){
        et.setText(Integer.toString(value));
    }





}